//
//  BonusCalculatorVC.m
//  DaleTou
//
//  Created by anyfish010 on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "BonusCalculatorVC.h"
#import "ZXTableView.h"
#import "XGConfig.h"
#import "BonusCell.h"
#import "ZPickerView.h"
#import "Appearance.h"
#import "DaleTouModel.h"
#import "DaletouViewController.h"
@interface BonusCalculatorVC ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>

@property(nonatomic,strong)ZXTableView *mainTable;
@property(nonatomic,strong)NSString *dateString;
@property(nonatomic,assign)int dateIndex;
@property(nonatomic,strong)NSString *hong1;
@property(nonatomic,strong)NSString *lan1;
@property(nonatomic,strong)NSString *hong2;
@property(nonatomic,strong)NSString *lan2;

@property(nonatomic,strong)DaleTouModel *model;

@end

@implementation BonusCalculatorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    [self setNavConfigStartColor:@"#F57321" endColor:@"#F57321"];
    [self ApptitileView:@"奖金计算器"];
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        self.mainTable.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"kaijiang.plist"];//这里就是你将要存储的沙盒路径（.plist文件，名字自定义）
    NSMutableArray *datearray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    if (datearray.count>0) {
        NSDictionary *dic = datearray.firstObject;
            DaleTouModel *model = [DaleTouModel new];
            model.redarray = [NSMutableArray array];
            model.bluearray = [NSMutableArray array];
            NSString *numberstr = [dic valueForKey:@"preDrawCode"];
            NSString *timestr = [dic valueForKey:@"preDrawTime"];
            NSString *qishu = [dic valueForKey:@"preDrawIssue"];
            model.qishu = qishu;
            model.timestr = [timestr substringWithRange:NSMakeRange(0, 10)];
            NSArray *allnumberarr = [numberstr componentsSeparatedByString:@","];
            if (allnumberarr.count==7) {
                NSArray *readarray = [allnumberarr subarrayWithRange:NSMakeRange(0, 5)];
                NSArray *bulearray = [allnumberarr subarrayWithRange:NSMakeRange(5, 2)];
                for (NSString *str in readarray) {
                    NSNumber *number = [NSNumber numberWithInteger:[str integerValue]];
                    [model.redarray addObject:number];
                }
                for (NSString *str in bulearray) {
                    NSNumber *number = [NSNumber numberWithInteger:[str integerValue]];
                    [model.bluearray addObject:number];
                }
            }
        _model = model;
            
            
        }
    
    self.hong1 = @"5";
    self.hong2 = @"0";
    self.lan1 = @"2";
    self.lan2 = @"0";
    self.dateIndex = 1;
    self.dateString = @"第2018051期";

    _mainTable = [[ZXTableView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, ScreenWidth, ScreenHeight - NAV_HEIGHT) style:UITableViewStyleGrouped];

    _mainTable.backgroundColor = [UIColor whiteColor];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    [self.view addSubview:self.mainTable];
    
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BonusCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell == nil){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"BonusCell" owner:nil options:nil] objectAtIndex:indexPath.section];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    

    
    if(indexPath.section == 0){
        cell.btnChoose.tag = 100;
        [cell.btnChoose addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
        cell.labDate.text = _dateString;
        cell.index = _dateIndex;
        if (_model) {
            cell.kaijianghaoma.text = [NSString stringWithFormat:@"最新开奖号码(%@期)",_model.qishu];
            for (UIButton *btn in cell.btnarray) {
                NSInteger tag = btn.tag - 1000;
                if (tag<5) {
                    [btn setTitle:[NSString stringWithFormat:@"%@",_model.redarray[tag]] forState:UIControlStateNormal];
                }else{
                    [btn setTitle:[NSString stringWithFormat:@"%@",_model.bluearray[tag-5]] forState:UIControlStateNormal];
                }
            }
        }
       
        
        
    }
    
    if(indexPath.section == 1){
        cell.btnAdd.layer.cornerRadius = cell.btnAdd.frame.size.height/2;
        cell.btnAdd.layer.masksToBounds = YES;
        cell.btn1.tag = 101;
        cell.btn2.tag = 102;
        cell.btn3.tag = 103;
        cell.btn4.tag = 104;

        [cell.btn1 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn2 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn3 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn4 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.labnumber1.text = [NSString stringWithFormat:@"%@个",self.hong1];
        cell.labnumber2.text = [NSString stringWithFormat:@"%@个",self.lan1];
        cell.labnumber3.text = [NSString stringWithFormat:@"%@个",self.hong2];
        cell.labnumber4.text = [NSString stringWithFormat:@"%@个",self.lan2];
        
        [cell.btnAdd addTarget:self action:@selector(gorate) forControlEvents:UIControlEventTouchUpInside];
        int number = 0;
        int money = 0;
        if(self.hong1.intValue < 5 || self.lan1.intValue < 2){
            number = 0;
            money = 0;
//            cell.labMoney.text = [NSString stringWithFormat:@"共%d注 %d元",number,money];
            cell.labMoney.hidden = YES;

        }else{
            
            int topNum = 1;
            for (int i = 6; i <= self.hong1.intValue; i ++) {
                int number = i;
                topNum = number * topNum;
            }
            
            int downNum = 1;
            for (int i = 1; i <= self.hong1.intValue - 5; i ++) {
                int number = i;
                downNum = number * downNum;
            }
            
            int HongNumber = topNum/downNum;
            
            int topNum1 = 1;
            for (int i = 3; i <= self.lan1.intValue; i ++) {
                int number = i;
                topNum1 = number * topNum1;
            }
            
            int downNum1 = 1;
            for (int i = 1; i <= self.lan1.intValue - 2; i ++) {
                int number = i;
                downNum1 = number * downNum1;
            }
            
            int HongNumber1 = topNum1/downNum1;
            
            number = HongNumber * HongNumber1;
            money = number * 2;
            
            int count = self.lan2.intValue + self.hong2.intValue;
            NSString *money1 = @"中0元";
            if(count == 2){
                money1 = @"中5元";
            }else if (count == 3){
                money1 = @"中10元";

            }else if (count == 4){
                money1 = @"中200元";

            }else if (count == 5){
                money1 = @"中3238元";

            }else if (count == 6){
                money1 = @"中23138元";

            }else if (count == 7){
                money1 = @"中3521525元";
            }
            cell.labBouns.text = money1;
            
        }
        

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 160 : 360;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? 0.1 : 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (void)onclick:(UIButton *)btn{
    NSInteger index = btn.tag - 100;
    NSArray *arr;
    switch (index) {
        case 0:
            arr = @[@"第2018051期",@"第2018052期",@"第2018053期",@"第2018054期",@"第2018055期",@"第2018056期",@"第2018057期",];
            break;
        case 1:{
            NSMutableArray *mArr = [NSMutableArray array];
            for (int i = 0; i <= 35; i ++) {
                [mArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
            arr = mArr;
        }
            break;
        case 2:{
            NSMutableArray *mArr = [NSMutableArray array];
            for (int i = 1; i <= 17; i ++) {
                [mArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
            arr = mArr;
        }
            
            break;
        case 3:{
            NSMutableArray *mArr = [NSMutableArray array];
            for (int i = 0; i <= 5; i ++) {
                [mArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
            arr = mArr;
        }
            
            break;
        case 4:{
            NSMutableArray *mArr = [NSMutableArray array];
            for (int i = 0; i <= 5; i ++) {
                [mArr addObject:[NSString stringWithFormat:@"%d",i]];
            }
            arr = mArr;
        }
            
            break;
        default:
            break;
    }
    [ZPickerView pickviewWithMode:EPickerModeSinger dataSource:arr block:^(NSObject *obj) {
        NSString *string = (NSString *)obj;
        if(index == 0){
            self.dateString = string;
            for (int i = 0; i < arr.count; i++) {
                NSString *string222 = [arr objectAtIndex:i];
                if([string222 isEqualToString:self.dateString]){
                    self.dateIndex = i;
                }
            }
        }else if (index == 1){
            self.hong1 = string;
        }else if (index == 2){
            self.lan1 = string;
        }else if (index == 3){
            self.hong2 = string;
        }else if (index == 4){
            self.lan2 = string;
        }
        
        [self.mainTable reloadData];
    }];
    [ZPickerView show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)gorate{
    DaletouViewController *vc = [[DaletouViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
